<?php


//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if ((($ext == "jpg")||($ext == "JPG") )&& ($_FILES["uploaded_file"]["type"] == "image/jpeg")&&($_FILES["uploaded_file"]["size"] < 2000000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/avatar/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place 
			if(($old != NULL)&&($old != 'defaultdp.png')){
			$do = unlink("avatar/$old");
			if($do)
			{
				echo "done deleting old pic!";
				}
			} //delete the current photo 
			$up_query = mysql_query("UPDATE `tbluser` SET avatar='$filename' WHERE email='$email'");//update avatar filename
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))&&($up_query)) {
    //image upload on forums accepts .bmp, .jpg, .jpeg, and .gif that limits the updload upto 1MB.
	
	$data = mysql_query("SELECT * from `tbluser` where email='$email'"); //where condition
	while($row = mysql_fetch_array($data)){	

	echo "<p style='text-align:center;'>You have successfully changed your picture!
	<table width='126px' height='86' border='0' align='center' cellpadding='5' cellspacing='0'  bgcolor='#ebf9b4'>
  <tr>
    <td>
	<div class='img_h'>
		<img src='avatar/"."$row[15]"."' width='150px' />
		<br/><center>Current Picture</center>
	</div>
	</td>
  </tr>
</table></p>
";  
        } }else {
           echo "Error: A problem occurred during file upload! <br/><br/>";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists <br/><br/>";
      }
  } else {
     echo "Error: Only .jpg images under 2MB are accepted for upload <br/><br/>";
  }
} else {
 echo "Error: No file uploaded <br/><br/>";
}

echo "<a href='profile.php?sel=profile'> <input id='bbtn'  type='button' value='Back' name='Back'/></a>";
?>

